package com.tiancheng.onecode.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tiancheng.onecode.entity.Platform;
import com.tiancheng.onecode.entity.PlatformMerchantRegInvite;
import com.tiancheng.onecode.exception.Asserts;
import com.tiancheng.onecode.mapper.PlatformMerchantRegInviteMapper;
import com.tiancheng.onecode.model.dto.MerchantRegistrationQrDTO;
import com.tiancheng.onecode.model.request.admin.PlatformMerchantRegInviteAddReq;
import com.tiancheng.onecode.model.request.admin.PlatformMerchantRegInviteUpdateReq;
import com.tiancheng.onecode.model.response.admin.PlatformQueryRes;
import com.tiancheng.onecode.service.IPlatformMerchantRegInviteService;
import com.tiancheng.onecode.service.IPlatformService;
import com.tiancheng.onecode.service.IQrService;
import jakarta.annotation.Resource;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Objects;

/**
 * @Author: kellen
 * @Description: 平台商户入驻入口配置 实现
 * @Date: create in 2024/12/23 16:34
 */
@Service
public class PlatformMerchantRegInviteServiceImpl extends ServiceImpl<PlatformMerchantRegInviteMapper, PlatformMerchantRegInvite> implements IPlatformMerchantRegInviteService {

    @Resource
    private IPlatformService platformService;
    @Resource
    private IQrService qrService;

    /**
     * 小程序页面地址
     */
    public static final String MIMI_APP_PAGE = "/index";

    @Override
    public PlatformMerchantRegInvite addNew(PlatformMerchantRegInviteAddReq param) {
        final PlatformQueryRes platformQueryRes = platformService.queryByPlatformCode(param.getPlatformCode());
        Asserts.isTrue(Objects.nonNull(platformQueryRes) && Objects.nonNull(platformQueryRes.getId()), "平台信息异常，平台不存在！");
        final List<PlatformMerchantRegInvite> list = this.list(new LambdaQueryWrapper<PlatformMerchantRegInvite>().eq(PlatformMerchantRegInvite::getPlatformCode, param.getPlatformCode()).eq(PlatformMerchantRegInvite::getIsDisable, 0));
        Asserts.isEmpty(list, "该平台已有在使用中的商户入驻指引，不能再添加！");

        final PlatformMerchantRegInvite platformMerchantRegInvite = new PlatformMerchantRegInvite();
        platformMerchantRegInvite.setPlatformCode(param.getPlatformCode());
        platformMerchantRegInvite.setWebUrl(param.getWebUrl());
        platformMerchantRegInvite.setCreatedDt(LocalDateTime.now());
        platformMerchantRegInvite.setRemark(param.getRemark());

        platformMerchantRegInvite.setIsDisable(0);
        // 生成微信端小程序码
        platformMerchantRegInvite.setClientUrl(getClientUrlByPlatformCode(platformQueryRes));
        this.save(platformMerchantRegInvite);
        return platformMerchantRegInvite;
    }

    @Override
    public void updateRecord(PlatformMerchantRegInviteUpdateReq params) {
        final PlatformQueryRes platformQueryRes = platformService.queryByPlatformCode(params.getPlatformCode());
        Asserts.isTrue(Objects.nonNull(platformQueryRes) && Objects.nonNull(platformQueryRes.getId()), "平台信息异常，平台不存在！");
        if(Objects.isNull(params.getId())){
            final List<PlatformMerchantRegInvite> list = this.list(new LambdaQueryWrapper<PlatformMerchantRegInvite>().eq(PlatformMerchantRegInvite::getPlatformCode, params.getPlatformCode()).eq(PlatformMerchantRegInvite::getIsDisable, 0));
            if(CollectionUtils.isEmpty(list)){
                // 新增一条
                PlatformMerchantRegInviteAddReq addReq = new PlatformMerchantRegInviteAddReq();
                addReq.setPlatformCode(params.getPlatformCode());
                addReq.setRemark(params.getRemark());
                addReq.setWebUrl(params.getWebUrl());
                addNew(new PlatformMerchantRegInviteAddReq());
                return;
            }
        }
        final PlatformMerchantRegInvite one = this.getById(params.getId());
        Asserts.isTrue(Objects.nonNull(one), "自助进件信息不存在！");
        final PlatformMerchantRegInvite update = new PlatformMerchantRegInvite();
        update.setId(params.getId());
        if (StringUtils.isNotEmpty(params.getWebUrl())) {
            update.setWebUrl(params.getWebUrl());
        }
        if (StringUtils.isNotEmpty(params.getRemark())) {
            update.setRemark(params.getRemark());
        }
        update.setClientUrl(getClientUrlByPlatformCode(platformQueryRes));
        update.setLastUpdDt(LocalDateTime.now());
        this.updateById(update);
    }

    /**
     * 根据平台信息获取小程序页面码
     *
     * @param platformQueryRes 平台编码
     * @return
     */
    private String getClientUrlByPlatformCode(PlatformQueryRes platformQueryRes) {
        // TODO 生成微信端小程序码
        final Platform platform = new Platform();
        platform.setPlatformCode(platformQueryRes.getPlatformCode());
        platform.setPlatformName(platformQueryRes.getPlatformName());
        final MerchantRegistrationQrDTO merchantRegistrationQrDTO = qrService.genMerchantRegistrationQr(platform);
        Asserts.isNotNull(merchantRegistrationQrDTO, "生成小程序进件页面码失败！请联系技术处理！");
        return merchantRegistrationQrDTO.getUrl();
    }
}
